package org.csu.xzypetstore.service;


import org.csu.xzypetstore.domain.Item;
import org.csu.xzypetstore.domain.LineItem;
import org.csu.xzypetstore.domain.Order;
import org.csu.xzypetstore.domain.Sequence;
import org.csu.xzypetstore.persistence.ItemMapper;
import org.csu.xzypetstore.persistence.lineItemMapper;
import org.csu.xzypetstore.persistence.orderMapper;
import org.csu.xzypetstore.persistence.sequenceMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@MapperScan("org.csu.xzypetstore.persistence")
public class orderService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private orderMapper orderMapper;

    @Autowired
    private sequenceMapper sequenceMapper;

    @Autowired
    private lineItemMapper lineItemMapper;

    public void insertOrder(Order order)
    {
        order.setOrderId(getNextId("ordernum"));
        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            String itemId = lineItem.getItemId();
            Integer increment = lineItem.getQuantity();
            System.out.println(increment+"   /////////.........................................");
            Map<String, Object> param = new HashMap<String, Object>(2);
            param.put("itemId", itemId);
            param.put("increment", increment);
            itemMapper.updateInventoryQuantity(param);
        }

        orderMapper.insertOrder(order);
        orderMapper.insertOrderStatus(order);
        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            lineItem.setOrderId(order.getOrderId());
            lineItemMapper.insertLineItem(lineItem);
        }
    }

    public int getNextId(String name) {
        Sequence sequence = new Sequence(name, -1);
        sequence = (Sequence) sequenceMapper.getSequence(sequence);
        if (sequence == null) {
            throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name
                    + " sequence).");
        }
        Sequence parameterObject = new Sequence(name, sequence.getNextId() + 1);
        if(sequenceMapper.updateSequence(parameterObject)){
            return parameterObject.getNextId();
        }else {
            throw new RuntimeException("Can't updateSequence!");
        }

    }
    public List<Order> getOrdersByUsername(String username) {
        return orderMapper.getOrdersByUsername(username);
    }

    public List<Order> getOrders() {
        return orderMapper.getOrders();
    }
    public Order getOrder(int orderId) {
        Order order = orderMapper.getOrder(orderId);
        order.setLineItems(lineItemMapper.getLineItemsByOrderId(orderId));

        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            Item item = itemMapper.GetItem(lineItem.getItemId());
            item.setQuantity(itemMapper.getInventoryQuantity(lineItem.getItemId()));
            lineItem.setItem(item);
        }

        return order;
    }

    public void deleteOrder(int orderId)
    {

        orderMapper.deleteOrder(orderId);
        orderMapper.deleteOrder1(orderId);
    }

    public Boolean postProduct(Order order)
    {
        return  orderMapper.postProduct(order);
    }

}

